package cn.tedu.baidashi.service.Impl;

import cn.tedu.baidashi.exception.ServiceException;
import cn.tedu.baidashi.mapper.AddressMapper;
import cn.tedu.baidashi.mapper.CustomerMapper;
import cn.tedu.baidashi.model.Address;
import cn.tedu.baidashi.model.Customer;
import cn.tedu.baidashi.service.IAddressService;
import cn.tedu.baidashi.vo.AddressVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2022-02-12
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
//    private List<Address> addresses=new CopyOnWriteArrayList<>();
    private List<Address> addresses=new ArrayList<>();
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CustomerMapper customerMapper;
//    public List<Address> getAll() {
//        addresses=addressMapper.selectList(null);
//        return addresses;
//    }
    public List<Address> findAddressVoByCustomerName(@AuthenticationPrincipal UserDetails userDetails){
        addresses=addressMapper.findAddressByCustomerName(userDetails.getUsername());
        return addresses;
    }

    @Override
    public List<Address> getAll() {
        return null;
    }
    //创建一个registerAddress对象通过列赋值来输入，接着调用addressMapper extend 的baseMapper来进行插入。
    @Override
    public void registerAddress(AddressVo addressVo, String customerName) {
        if(customerName.equals("朱锦莹")){
            Address address=new Address()
                    .setCustomerName(addressVo.getCustomerName())
                    .setProvinceName(addressVo.getProvinceName())
                    .setCityName(addressVo.getCityName())
                    .setZoneName(addressVo.getZoneName())
                    .setPhone(addressVo.getPhone())
                    .setPostcode(addressVo.getPostcode())
                    .setStatus(addressVo.getStatus())
                    .setStreet(addressVo.getStreet())
                    .setCustomerId(1);
            int num=addressMapper.insert(address);
            if(num!=1){
                throw new ServiceException("数据库异常");
            }
        }else {
            Address address=new Address()
                    .setCustomerName(addressVo.getCustomerName())
                    .setProvinceName(addressVo.getProvinceName())
                    .setCityName(addressVo.getCityName())
                    .setZoneName(addressVo.getZoneName())
                    .setPhone(addressVo.getPhone())
                    .setPostcode(addressVo.getPostcode())
                    .setStatus(addressVo.getStatus())
                    .setStreet(addressVo.getStreet())
                    .setCustomerId(2);
            int num=addressMapper.insert(address);
            if(num!=1){
                throw new ServiceException("数据库异常");
            }

        }

    }
    @Override
    public Boolean removeAddress(Integer id, String customerName) {
        Address address=addressMapper.selectById(id);
        int num=0;
        Customer customer=customerMapper.findIdByCustomerName(customerName);
        if(address.getCustomerId()==customer.getId()){
            num=addressMapper.deleteById(id);
        }
            return num==1;
//        Address address=customerMapper.findAddressIdByCustomerName(customerName);
//        QueryWrapper<Address> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("id",id);
//        Address address1=address.
//        int num=0;
//        if(id.equals(address.getId())){
//
//            Address a=addressMapper.selectById(id);
//            num=addressMapper.deleteById(a.getId());
//
//        }
//        return num==1;
    }

    @Override
    public Address selectAddressToFormById(Integer id) {
        Address address=addressMapper.selectById(id);
        return address;
    }

    @Override
    public void updateAddress(AddressVo addressVo, String customerName) {
        Customer customer=customerMapper.findIdByCustomerName(customerName);
                Address address1=new Address()
                .setCustomerName(addressVo.getCustomerName())
                .setProvinceName(addressVo.getProvinceName())
                .setCityName(addressVo.getCityName())
                .setZoneName(addressVo.getZoneName())
                .setPhone(addressVo.getPhone())
                .setStreet(addressVo.getStreet())
                .setStatus(addressVo.getStatus())
                .setPostcode(addressVo.getPostcode())
                .setCustomerId(customer.getCustomerName()=="朱锦莹"?1:2)
                .setId(addressVo.getId());
        int num=addressMapper.updateById(address1);
        if(num!=1){
            throw new ServiceException("数据库异常！");
        }

    }

//    @Override
//    public void updateAddress(AddressVo address,Integer id) {
//        Address address1=new Address()
//                .setCustomerName(address.getCustomerName())
//                .setProvinceName(address.getProvinceName())
//                .setCityName(address.getCityName())
//                .setZoneName(address.getZoneName())
//                .setPhone(address.getPhone())
//                .setStreet(address.getStreet())
//                .setStatus(address.getStatus())
//                .setPostcode(address.getPostcode())
//                .setCustomerId(id);
//        int num=addressMapper.updateById(address1);
//        if(num!=1){
//            throw new ServiceException("数据库异常！");
//        }
//    }


}
